<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>会员充值</el-breadcrumb-item>
        <el-breadcrumb-item>会员购买配置</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
        @row-update="rowUpdate"
        @row-save="rowSave"
        @row-del="rowDel"
      >
        <template slot="last" slot-scope="scope">
          {{ scope.row.edit_time }}
          <el-tag>{{ scope.row.edit_admin_name }}</el-tag>
        </template>
        <!-- 自定义操作栏 -->
        <template slot-scope="scope" slot="menu">
          <el-button
            v-if="scope.row.flag == 0"
            icon="el-icon-primary"
            type="text"
            size="small"
            @click.stop="updateFlagFn(scope)"
            >设为默认</el-button
          >
          <el-button
            v-else
            icon="el-icon-primary"
            type="text"
            size="small"
            @click.stop="updateFlagFn(scope)"
            >已设默认</el-button
          >
        </template>
        <!-- 文字角标 -->
        <template slot-scope="scope" slot="character_corner">
          <el-row
            justify="center"
            v-if="
              scope.row.character_corner != undefined && scope.row.character_corner.trim().length != 0
            "
          >
            <el-descriptions-item>{{ scope.row.character_corner }}</el-descriptions-item>
          </el-row>
          <el-row justify="center">
            <el-button
              icon="el-icon-circle-plus"
              type="text"
              size="small"
              @click.stop="rowEdit(scope.row)"
              v-if="
                scope.row.character_corner == undefined || scope.row.character_corner.trim().length == 0
              "
            >
              <font>添加文案</font>
            </el-button>
            <el-button
              icon="el-icon-s-order"
              type="text"
              size="small"
              style="color: red"
              @click.stop="getMultilingualDetail(scope.row, 1)"
              v-else-if="scope.row.multilingual1 == 0"
            >
              <font>配置未完善</font>
            </el-button>
            <el-button
              icon="el-icon-s-order"
              type="text"
              size="small"
              @click.stop="getMultilingualDetail(scope.row, 1)"
              v-else
            >
              <font>配置多语言文本</font>
            </el-button>
          </el-row>
        </template>
        <!-- 文案 -->
        <template slot-scope="scope" slot="text">
          <el-row
            justify="center"
            v-if="
              scope.row.text != undefined && scope.row.text.trim().length != 0
            "
          >
            <el-descriptions-item>{{ scope.row.text }}</el-descriptions-item>
          </el-row>
          <el-row justify="center">
            <el-button
              icon="el-icon-circle-plus"
              type="text"
              size="small"
              @click.stop="rowEdit(scope.row)"
              v-if="
                scope.row.text == undefined || scope.row.text.trim().length == 0
              "
            >
              <font>添加文案</font>
            </el-button>
            <el-button
              icon="el-icon-s-order"
              type="text"
              size="small"
              style="color: red"
              @click.stop="getMultilingualDetail(scope.row, 2)"
              v-else-if="scope.row.multilingual2 == 0"
            >
              <font>配置未完善</font>
            </el-button>
            <el-button
              icon="el-icon-s-order"
              type="text"
              size="small"
              @click.stop="getMultilingualDetail(scope.row, 2)"
              v-else
            >
              <font>配置多语言文本</font>
            </el-button>
          </el-row>
        </template>
      </avue-crud>
    </basic-container>
    <!-- 弹出框 -->
    <el-dialog
      title="多语言文本配置"
      :visible.sync="multilingualDialogVisible"
      :append-to-body="true"
    >
      <el-form :model="multilingual_data" label-width="120px">
        <el-form-item label="文本说明：">
          <el-descriptions-item>{{
            multilingual_data.txt
          }}</el-descriptions-item>
        </el-form-item>

        <el-form-item label="动态文本：">
          <el-select
            v-model="multilingual_data.dynamic_able"
            placeholder="请选择是否为动态文本"
          >
            <el-option label="否" :value="0"></el-option>
            <el-option label="是" :value="1"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="英语文本：" prop="bookIntro">
          <el-input
            type="textarea"
            v-model="multilingual_data.en_txt"
            placeholder="请输入英语文本"
          ></el-input>
        </el-form-item>

        <el-form-item label="芬兰语文本：" prop="bookIntro">
          <el-input
            type="textarea"
            v-model="multilingual_data.fn_txt"
            placeholder="请输入芬兰语文本"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="multilingualDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateMultilingual(multilingual_data.updateIndex)">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { common } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";
import {
  appConfigVipPremium as baseURL,
  selectVersion,
  appConfigDefaultTxt,
} from "@/api/cat/baseUrls";
export default {
  data() {
    return {
      page: {
        total: 0,
        pageSize: 10,
      },
      type: true,
      data: [],
      multilingualDialogVisible: false,
      multilingual_data: {
        txt: "",
        use_area: 1,
        dynamic_able: 0,
        config_complete: 0,
        en_txt: "",
        fn_txt: "",
      },
      params: {},
      form: {},
      default: {
        orderByList: "sort&ASC,create_time&DESC",
      },
      option: {
        addBtn: true,
        editBtn: true,
        viewBtn: false,
        delBtn: true,
        searchBtn: true,
        refreshBtn: true,
        excelBtn: false,
        column: [
          {
            label: "版本号",
            prop: "app_version",
            type: "select",
            search: true,
            span: 12,
            placeholder: "请输入版本号",
            dicUrl: `${catUrl}/${selectVersion.selectVersion}`,
            props: {
              label: "app_version",
              value: "app_version",
            },
            dicFormatter: (res) => {
              return res.source;
            },
            rules: [
              {
                required: true,
                message: "版本号必须填写",
              },
            ],
          },
          {
            label: "用户端",
            prop: "user_port",
            type: "select",
            search: true,
            span: 13,
            dicData: [
              {
                label: "Android",
                value: 0,
              },
              {
                label: "IOS",
                value: 1,
              },
            ],
            props: {
              label: "label",
              value: "value",
            },
            rules: [
              {
                required: true,
                message: "用户端必须填写",
              },
            ],
          },
          {
            label: "会员卡类型",
            prop: "vip_type",
            type: "select",
            search: true,
            placeholder: "会员卡类型",
            dicData: [
              {
                label: "月卡",
                value: "0",
              },
              {
                label: "季卡",
                value: "1",
              },
              {
                label: "年卡",
                value: "2",
              },
              {
                label: "连续包月",
                value: "3",
              },
              {
                label: "连续包季",
                value: "4",
              },
              {
                label: "连续包年",
                value: "5",
              },
            ],
            props: {
              label: "label",
              value: "value",
            },
            rules: [
              {
                required: true,
                message: "会员卡类型必须选择",
              },
            ],
          },
          {
            label: "价格(元)",
            prop: "price",
            type: "input",
            controls:false,
            search: false,
            placeholder: "请输入价格（元）",
            rules: [
              {
                required: true,
                message: "价格必须填写",
              },
            ],
          },
          {
            label: "划线价",
            prop: "line_price",
            type: "input",
            controls:false,
            search: false,
          },
          {
            label: "排序",
            prop: "sort",
            type: "input",
          },
          {
            label: "文字角标",
            prop: "character_corner",
            type: "input",
            search: false,
            slot: true,
            placeholder: "请输入文字角标",
          },
          {
            label: "赠送天数",
            prop: "giveDays",
            type: "input",
            type: "number",
            search: false,
          },
          {
            label: "不打折价格商品id",
            prop: "noDiscountGoods",
            type: "input",
            search: false,
            hide: true,
          },
          {
            label: "文案",
            prop: "text",
            type: "input",
            search: false,
            slot: true,
          },
          // {
          //   label: "优惠折扣",
          //   prop: "discount",
          //   type: "input",
          //   search: false,
          // },
          {
            label: "9折商品价格",
            prop: "discount_price",
            type: "input",
            align: "center",
            search: false,
          },
          {
            label: "9折商品id",
            prop: "discountGoods",
            type: "input",
            search: false,
            hide: true,
          },
          {
            label: "最后操作信息",
            prop: "last",
            type: "input",
            slot: true,
            addDisplay: false,
            editDisplay: false,
            addDisabled: true,
            editDisabled: true,
          },
        ],
      },
    };
  },
  methods: {
    async updateFlagFn(scope) {
      this.$confirm("此操作同群组同版本仅存在一组默认 是否继续?", "提示", {
        type: "warning",
      })
        .then(async () => {
          let response = await common(
            baseURL.updateFlag,
            { vipId: scope.row.vip_id },
            null,
            "GET"
          );
        })
        .then(() => {
          this.getList();
        });
    },
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        baseURL.pageAndParam,
        { ...this.params, ...this.default },
        this.page,
        "POST"
      );
      //将接收到的值遍历
      if (response.data) {
        response.data.forEach((item) => {
          if (item.vip_gift) {
            //定义属性值接收
            //折扣
            item["discount"] = item.vip_gift.discount;
            //赠送天数
            item["giveDays"] = item.vip_gift.giveDays;
          }
          if (item.user_port == 0) {
            //定义属性值接收
            //不打折价格谷歌商品id
            item["noDiscountGoods"] = item.google_product_id.noDiscountGoods;
            //打折价格谷歌商品id
            item["discountGoods"] = item.google_product_id.discountGoods;
          } else if (item.user_port == 1) {
            //不打折价格谷歌商品id
            item["noDiscountGoods"] = item.apple_product_id.noDiscountGoods;
            //打折价格谷歌商品id
            item["discountGoods"] = item.apple_product_id.discountGoods;
          }
        });
      }
      this.page = response.page;
      for (let index in response.data) {
        let row = response.data[index];
        if (row.character_corner == undefined || row.character_corner.trim().length == 0) {
          row.multilingual1 = -1;
        } else {
          let multilingualConfig = await common(
            `${appConfigDefaultTxt.selectAppConfigDefaultTxt}?txt=${row.character_corner}&showDetail=false`,
            null,
            null,
            "GET"
          );
          if (multilingualConfig.data.config_complete == 1) {
            row.multilingual1 = 1;
          } else {
            row.multilingual1 = 0;
          }
        }
        if (row.text == undefined || row.text.trim().length == 0) {
          row.multilingual2 = -1;
        } else {
          let multilingualConfig = await common(
            `${appConfigDefaultTxt.selectAppConfigDefaultTxt}?txt=${row.text}&showDetail=false`,
            null,
            null,
            "GET"
          );
          if (multilingualConfig.data.config_complete == 1) {
            row.multilingual2 = 1;
          } else {
            row.multilingual2 = 0;
          }
        }
      }
      this.data = response.data;
      this.loading = false;
    },
    /**保存 */
    async rowSave(row, done, loading) {
      delete row["id"];
      let userInfo = this.$store.state.user.userInfo;
      row.edit_admin_name = userInfo.username;
      row.edit_admin_id = userInfo.id;
      this.convert(row);
      let data = await common(baseURL.save, row, null, "POST");
      if (data == null) {
        loading();
        return;
      }
      this.getList();
      done();
    },
    /**编辑 */
    rowEdit(row) {
      this.$refs.crud.rowEdit(row, row.$index);
    },
    /**修改 */
    async rowUpdate(row, index, done, loading) {
      let userInfo = this.$store.state.user.userInfo;
      row.edit_admin_name = userInfo.username;
      row.edit_admin_id = userInfo.id;
      this.convert(row);
      let data = await common(baseURL.update, row, null, "POST");
      if (data == null) {
        loading();
        return;
      }
      this.getList();
      done();
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    //合并折扣和赠送天数属性
    convert(row) {
      if (row) {
        //字段接收
        row["vip_gift"] = {
          discount: row.discount,
          giveDays: row.giveDays,
        };
        //字段接收
        if (row.user_port == 1) {
          row["apple_product_id"] = {
            noDiscountGoods: row.noDiscountGoods,
            discountGoods: row.discountGoods,
          };
          row["google_product_id"] = {
            noDiscountGoods: null,
            discountGoods: null,
          };
        } else if (row.user_port == 0) {
          row["google_product_id"] = {
            noDiscountGoods: row.noDiscountGoods,
            discountGoods: row.discountGoods,
          };
          row["apple_product_id"] = {
            noDiscountGoods: null,
            discountGoods: null,
          };
        }
      }
    },
    /**删除 */
    rowDel(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        type: "warning",
      })
        .then(async () => {
          let response = await common(
            baseURL.delete,
            { id: row.vip_id },
            null,
            "GET"
          );
        })
        .then(() => {
          this.getList();
        });
    },
    /**搜索 */
    searchChange(params, done) {
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
    /**获取多语言配置 */
    async getMultilingualDetail(row, type) {
      let updateTxt;
      let updateIndex;
      if (type == 1) {
        updateTxt = row.character_corner;
        updateIndex = 1;
      } else {
        updateTxt = row.text;
        updateIndex = 2;
      }
      let multilingual_res = await common(
        `${appConfigDefaultTxt.selectAppConfigDefaultTxt}?txt=${updateTxt}&showDetail=true`,
        null,
        null,
        "GET"
      );
      if (
        multilingual_res.data == undefined ||
        multilingual_res.data.id == undefined
      ) {
        this.multilingual_data = {
          txt: updateTxt,
          use_area: 1,
          dynamic_able: 0,
          config_complete: 0,
          en_txt: "",
          fn_txt: "",
          type: "add",
          rowId: row.vip_id,
          updateIndex: updateIndex,
        };
      } else {
        this.multilingual_data = multilingual_res.data;
        this.multilingual_data.type = "edit";
        this.multilingual_data.rowId = row.vip_id;
        this.multilingual_data.updateIndex = updateIndex;
      }
      this.multilingualDialogVisible = true;
    },
    /**修改多语言配置 */
    async updateMultilingual(type) {
      let userInfo = this.$store.state.user.userInfo;
      let edit_admin_id = userInfo.id;
      let edit_admin_name = userInfo.username;
      let update;
      if (this.multilingual_data.type == "add") {
        update = await common(
          appConfigDefaultTxt.save,
          { ...this.multilingual_data, edit_admin_id, edit_admin_name },
          null,
          "POST"
        );
      } else {
        update = await common(
          appConfigDefaultTxt.update,
          { ...this.multilingual_data, edit_admin_id, edit_admin_name },
          null,
          "POST"
        );
      }
      if (update.code == 1) {
        this.data.forEach((item) => {
          if (item.vip_id == this.multilingual_data.rowId) {
            if (type == 1) {
              item.multilingual1 = update.data.config_complete;
            } else {
              item.multilingual2 = update.data.config_complete;
            }
          }
        });
        this.multilingualDialogVisible = false;
      }
    },
  },
};
</script>